package com.aysafety.iotdataplatformserver.listener;

import com.aysafety.core.common.Common;
import com.aysafety.iotdataplatformserver.dao.KsTreeDataDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

@Component
public class KsTreeDataListener {
    private final String tableTreeName = "ksTreeData";

    private final String[] tableTreeStructure = new String[] {"ksTreeData"};

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    KsTreeDataDao ksTreeDataDao;

    public void generateTreeData(String entId, String type, String workingFace, String deviceId, Date datetime,
        String pos) throws IOException {
        List<String> params = new ArrayList<String>();
        params.add(entId);
        params.add(type);
        params.add(workingFace);
        params.add(deviceId);
        if (StringUtils.isNotBlank(pos)) {
            params.add(pos);
        }
        String rowKey = Common.getRowKey(params, "");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tableId",this.tableTreeName);
        map.put("pk", "\'" + rowKey + "\'");
        map.put("entId", "\'" + entId + "\'");
        map.put("type", "\'" +type + "\'");
        map.put("workingFace", "\'" + workingFace + "\'");
        map.put("deviceId", "\'" + deviceId + "\'");
        map.put("datetime", datetime.getTime());
        map.put("pos", "\'" + pos + "\'");
        ksTreeDataDao.insertKsTreeData(map);
    }
}
